package com.yntx.usercenter.common.exception;

import com.yntx.usercenter.common.util.JsonUtil;
import com.yntx.usercenter.common.web.Status;

import java.util.HashMap;
import java.util.Map;

/**
 * Description: 业务异常
 *
 * @author Liziba
 * @version 1.0
 * @date 2022/11/10 5:20 PM
 * @since JDK 1.8
 */
public final class BizException extends RuntimeException {

    private int code;

    private String message;

    private ErrorParam[] errorParams;

    public BizException(String message) {
        this.code = Status.Common.COMMON_ERROR.getCode();
        this.message = message;
    }

    public BizException(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public BizException(int code, String message, ErrorParam... errorParams) {
        this.code = code;
        this.message = message;
        this.errorParams = errorParams;
    }

    public int getCode() {
        return code;
    }

    @Override
    public String getMessage() {
        Map<String, Object> msgMap = new HashMap<>();
        msgMap.put("code", code);
        msgMap.put("message", message);
        msgMap.put("errorParams", errorParams);

        return JsonUtil.toJson(msgMap);
    }
}
